package com.aote.model.block;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.browsesoft.Entity;
import com.browsesoft.EntityManager;
import com.browsesoft.Tools;
import com.browsesoft.dbtools.DBTools;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLFormComponent;
import com.browsesoft.htmlcomponent.HTMLTableModel;
import com.browsesoft.parameter.Parameter;
import com.browsesoft.user.Role;

/**
 * sql表体数据model
 * 
 */
public class SqlBlockModel implements HTMLTableModel {
	/**
	 * 组件
	 */
	protected HTMLBasicComponent component = null;

	/**
	 * 数据，List的每一项为参数值对象
	 */
	protected List datas = new LinkedList();

	public void init(HttpServletRequest request) throws Exception {
		
	}
	
	 

	private List getDatas(String sql) {
		try {
			List result = new LinkedList();
			String[][] datas = DBTools.executeQueryWithTableHead(sql);
			for (int i = 1; i < datas.length; i++) {
				String str = datas[i][0];
				result.add(str);
			}
			return result;
		} catch (Exception e) {
			throw new RuntimeException("sql:" + sql + "错误", e);
		}
	}

	private List filterDatas(String id) {
		List result = new LinkedList();
		result.addAll(this.datas);
		String condition = this.getCondtionAtt(id);
		if (condition == null || condition.equals("")) {
			return result;
		}
		// 过虑
		Iterator iter = this.datas.iterator();
		while (iter.hasNext()) {
			String value = (String) iter.next();
			if (value.compareTo(condition) < 0) {
				result.add(value);
			}
		}
		return result;
	}

	private String getCondtionAtt(String id) {
		Entity entity = (Entity) EntityManager.getInstance().getObject(id);
		// 得到 配置得属性名
		String attrName = this.component.getElement().getAttribute(
				"parentiEntityAttr");
		String result = (String) entity.getAttributes().get(attrName);
		return result;
	}

	public void setComponent(HTMLBasicComponent component) {
		this.component = component;
	}

	public int getColumnCount() {
		return this.datas.size();
	}

	public String getColumnName(int index) {
		return (String) this.datas.get(index);
	}

	public int getRowCount() {
		return 0;
	}

	public void setCondition(String condition) {
	}

	public void modifyCellValue(Element td, Document doc, int row, int col) {
	}

	public String getType(int col) {
		return null;
	}

	public void updatePageElement(HTMLBasicComponent c) throws Exception {
	}

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List result = new LinkedList();
		//配置pro="a.b.c"  来显示左表头内容
		if(this.component.getElement().hasAttribute("pro")){
			String pro=this.component.getElement().getAttribute("pro");
			String[] value=pro.split("\\.");
			for(int i=0;i<value.length;i++){
				result.add(value[i]);
			}
			this.datas=result;
			//根据role从角色中得到要显示的集合，话务员话务量报表模版 中用到
		}else if(this.component.getElement().hasAttribute("role")){
			String value=this.component.getElement().getAttribute("role");
			Role role= EntityManager.getInstance().getRoleFromName(value);
			List temp = role.getUsers();
			for(int i=0;i<temp.size();i++){
				result.add(temp.get(i).toString());
			}
			this.datas=result;
			//根据参数值现在左表头 表故障汇总模版  中用到
		}else if(this.component.getElement().hasAttribute("param")){
			Parameter parameter = new Parameter(this.component.getElement()
					.getAttribute("param"));
			LinkedList list = parameter.getParamValues();
			for(int i=0;i<list.size();i++){
				result.add(list.get(i).toString());
			}
			this.datas=result;
			
		}else{
			// 根据sql查找数据
			String sql = this.component.getElement().getAttribute("sql");
			Entity entity = this.getEntityOfFormComponent();
			sql = Tools.replaceAttrs(sql, entity.getAttributes());
			System.out.println(entity.getAttributes());
			System.out.println("sql------"+sql);
			// 替换sql中的属性
			this.datas = getDatas(sql);
		}
		// 如果有id，说明是历史数据，根据配置的属性名称，得到限制属性值
		String id = request.getParameter("id");
		if (id != null && !id.equals("")) {
			this.datas = filterDatas(id);
		}
		
	}

	/**
	 * 得到最上层form组件的model
	 */
	public Entity getEntityOfFormComponent() {
		HTMLBasicComponent parent = this.component.getParent();
		while (parent != null) {
			if (parent instanceof HTMLFormComponent) {
				return (Entity) parent.getModel();
			}
			parent = parent.getParent();
		}
		return null;
	}
}
